package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.comn.model.ConvenientLifeQiuZhiInfo;
import com.mall507.comn.model.ConvenientLifeZhaoPinInfo;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.xdialog.XAddressDialog;

import org.json.JSONException;
import org.json.JSONObject;

@Route(path = "/main/MainQiuZhiInfoUI")
public class MainQiuZhiInfoUI extends BaseUI {
    @Autowired(name = "id")
    String id;
    @ViewInject(R.id.head_img)
    CircleImageView head_img;
    @ViewInject(R.id.name)
    TextView name;
    @ViewInject(R.id.sex)
    TextView sex;
    @ViewInject(R.id.age)
    TextView age;
    @ViewInject(R.id.time)
    TextView time;
    @ViewInject(R.id.type)
    TextView type;
    @ViewInject(R.id.salary)
    TextView salary;
    @ViewInject(R.id.position)
    TextView position;
    @ViewInject(R.id.contacts)
    TextView contacts;
    @ViewInject(R.id.phone)
    TextView phone;
    @ViewInject(R.id.describe)
    TextView describe;
    @ViewInject(R.id.address)
    TextView address;
    @ViewInject(R.id.call)
    Button call;
    @ViewInject(R.id.zhiye)
    TextView zhiye;
    @ViewInject(R.id.dao)
    Button dao;
    String Address = "";
    String Addresslat = "";
    String Addresslng = "";

    String callNumber = "";

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_main_qiuzhi_info);
        getTitleView().setContent("求职详情");
        showLoding();
        loadNetWorkData();
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = phone.getText().toString();
                callNumber = str.substring(5,str.length());
                call(callNumber);
            }
        });
        dao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XAddressDialog().show(MainQiuZhiInfoUI.this, Address, Addresslng,Addresslat,new XAddressDialog.XUserMainSignRewardDialogCallBack() {
                    @Override
                    public void confirm() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
            }
        });
    }

    private void loadNetWorkData() {
        showLoding();
        BaseHttp.getInstance().query("release/releaseDetails", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    dismissLoding();
                    JSONObject list = response.optJSONObject("data");
                    String xingbie = list.optString("xingbie").equals("0")?"男":"女";
                    head_img.setImageURI(list.optString("head_img"));
                    name.setText(list.optString("contacts"));
                    sex.setText("性别：" + xingbie + "   " + "年龄：" + list.optString("nianling") + "   " + "发布：" + list.optString("posttime"));
                    type.setText("求职类型：" + list.optString("xingzhi"));
                    salary.setText("期望薪资：" + list.optString("xinzi"));
                    position.setText("期望职位：" + list.optString("qiwangzhiwei"));
                    Address = list.optString("address");
                    Addresslat = list.optString("lat");
                    Addresslng = list.optString("lng");
                    address.setText("期望地址：" + list.optString("address"));
                    describe.setText(list.optString("content"));
                    contacts.setText("联系人：" + list.optString("contacts"));
                    phone.setText("联系电话：" + list.optString("tel"));
                    zhiye.setText("期望职业：" + list.optString("occupation_name"));
                }
//                ConvenientLifeQiuZhiInfo convenientLifeQiuZhiInfo = new Gson().fromJson(response.toString(), ConvenientLifeQiuZhiInfo.class);
//                handleData(convenientLifeQiuZhiInfo);
                dismissLoding();
            }
        });
    }

    private void handleData(ConvenientLifeQiuZhiInfo convenientLifeQiuZhiInfo) {
        if (convenientLifeQiuZhiInfo.getCode().equals("200")) {
            dismissLoding();
            ConvenientLifeQiuZhiInfo.DetailsBean details = convenientLifeQiuZhiInfo.getDetails();
            String xingbie = details.getXingbie().equals("0")?"男":"女";
            head_img.setImageURI(details.getHead_img());
            name.setText(details.getContacts());
            sex.setText("性别：" + xingbie + "   " + "年龄：" + details.getNianling() + "   " + "发布：" + details.getPosttime());
            type.setText("求职类型：" + details.getXingzhi());
            salary.setText("期望薪资：" + details.getXinzi());
            position.setText("期望职位：" + details.getQiwangzhiwei());
            Address = details.getAddress();
            address.setText("期望地址：" + details.getAddress());
            describe.setText(details.getContent());
            contacts.setText("联系人：" + details.getContacts());
            phone.setText("联系电话：" + details.getTel());
            zhiye.setText("期望职业：" + details.getOccupation_name());
        }
        dismissLoding();
    }

    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    private void call(String phoneNum){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
